package com.moli.iov.handle.real.convertor;

import com.moli.iov.handle.real.util.Contact;
import com.moli.iov.protocol.codec.DataDecoder;
import com.moli.iov.protocol.model.realtime.*;
import com.moli.iov.util.ByteArrayUtil;
import com.moli.iov.util.JsonUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName Convert
 * @Description 转换器
 * @Author 李伟松
 * @CreateDate 2019-02-26 下午 5:13
 * @UpdateDate 2019-02-26 下午 5:13
 * @Version 1.0
 */
public class Convert {


    public static void main(String[] args) throws Exception {
        RealTimeInfoReq req = new RealTimeInfoReq();
        String report = "232302fe4c4e425343423346324a5731383530333501017913061312352b010203010000000214120df427106202002710000002010104534e204e20520dc02710050106bba5cd0156fc0e060101107c0114106801014a010249070000000000000000000801010df427100054000154107c107c107c107c107c107c107c107c107c107c107c107c107c107c107c107c107c107c107c1068107c107c107c1068107c107c107c107c107c107c107c107c107c107c107c107c107c107c107c107c107c107c107c107c107c107c107c107c107c107c107c107c107c107c107c107c107c107c107c107c107c107c107c107c107c107c107c107c107c107c107c107c107c107c107c107c107c107c107c107c107c107c107c107c090101000e4a494a4a4a4a4a4a494a4a4a4a4aa000630002010014001d0000fe00008b0100000033c90000012800170200470705c0f74cd1a58ca4b05405c10000000044c0000005c25b5a27105251072505c3015e45c40701021405c407eb00000000353505c5404001540e00360405c6020c018240000000f7";
        byte[] bytes = ByteArrayUtil.hexStringToBytes(report);
        DataDecoder<RealTimeInfoReq> decoder = new DataDecoder<RealTimeInfoReq>();
        decoder.decode(req, bytes);
        System.out.println(toRealTimeInfoDtoJson("LNBSCB3F3JW182760",req,new Date()));
    }
    /**
     * 将报文对象转换为业务json
     */
    public static String toRealTimeInfoDtoJson(String vin, RealTimeInfoReq realTimeInfoReq, Date collectDate) {

        if (collectDate != null && realTimeInfoReq != null) {
            Map<String, Object> map = new HashMap<String, Object>();

            map.put(Contact.VIN, vin);
            //采集时间
            map.put(Contact.COLLECT_DATE, collectDate);

            map.put(Contact.REPORT_DATE, realTimeInfoReq.collectDate == null ? null : realTimeInfoReq.collectDate.getDate());

            //GPS数据
            VehicleGpsReq gpsReq = realTimeInfoReq.getVehicleGpsReq();

            if (gpsReq != null) {
                map.put(Contact.VEHICLE_GPS, gpsReq);
            }
            //整车数据
            VehicleAllReq allReq = realTimeInfoReq.getVehicleAllReq();
            if (allReq != null) {
                map.put(Contact.VEHICLE_ALL, allReq);
            }

            //扩展数据
            ExtendReq extendReq = realTimeInfoReq.getExtendReq();
            if (extendReq != null) {
                //扩展车辆状态数据
                ExtendVehicleState extendVehicleState = extendReq.getextendVehicleState();
                if (extendVehicleState != null) {
                    //扩展车辆状态数据
                    map.put(Contact.EXTEND_VEHICLE_STATE, extendVehicleState);
                }
            }
            ;

            //驱动电机数据
            DriveMotorReq motorReq = realTimeInfoReq.getDriveMotorReq();
            if (motorReq != null) {
                //驱动电机数据
                map.put(Contact.DRIVE_MOTOR, motorReq);
            }

            LimitValueReq limitValueReq = realTimeInfoReq.getLimitValueReq();
            if(limitValueReq!=null){
                //驱动电机数据
                map.put(Contact.LIMIT_VALUE, limitValueReq);
            }

            BmsTemperatureReq bmsTemperature = realTimeInfoReq.getBmsTemperatureReq();
            if(bmsTemperature!=null){
                //可充电储能电流
                map.put(Contact.BMS_TEMPERATURE, bmsTemperature);
            }
            BmsVoltageReq bmsVoltage = realTimeInfoReq.getBmsVoltageReq();
            if(bmsVoltage!=null){
                //电池单体电压数据
                map.put(Contact.BMS_VOLTAGE, bmsVoltage);
            }

            return JsonUtils.toJson(map);
        }
        return null;
    }


}
